<?php

namespace app\logic\model\user;

use app\logic\model\LogicModel;
use app\logic\model\music\MusicLogicModel;

class UserLogicModel extends LogicModel
{
    protected $name="user";

    public static function getIdsByName($name)
    {
        return self::whereLike('nickname|writer_name|true_name',$name)->column('id');
    }
    public static function getIdsByAggregate($keyword)
    {
        return self::whereLike('nickname|writer_name|true_name|mobile',$keyword)->column('id');
    }
    public static function getIdsByWriterName($name)
    {
        return self::whereLike('writer_name',$name)->column('id');
    }
    public static function getIdsByWriterNameKey($name)
    {
        return self::whereLike('writer_name',$name)->column('id','writer_name');
    }
    public static function getInfoById( $id,$filed='*')
    {
        return self::where('id',$id)->field($filed)->find();
    }
    public static function getInfoByOpenid( $openid,$filed='*')
    {
        return self::where('openid',$openid)->field($filed)->find();
    }
    public static function getInfoByobile( $mobile,$filed='*')
    {
        return self::where('mobile',$mobile)->field($filed)->find();
    }
    public static function getInfosByIds( $id=[],$filed='*')
    {
        return self::whereIn('id',$id)->column($filed,'id');
    }
    public static function getListByMusicId( $music_id,$filed)
    {
        $user_id = MusicLogicModel::getUserIdByMusicId($music_id);
        return self::whereIn('id',$user_id)->column($filed,'id');
    }

    public static function getWriterRate($sell_id)
    {
        return self::where('is_writer',1)
            ->where('id',$sell_id)
            ->value('writer_rate')??0;
    }

    function getList($request,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'nickname')){
                    $q->whereLike("nickname","%{$request['nickname']}%");
                }
                if(check_request($request,'writer_name')){
                    $q->whereLike("writer_name","%{$request['writer_name']}%");
                }
                if(check_request($request,'true_name')){
                    $q->whereLike("true_name","%{$request['true_name']}%");
                }
                if(check_request($request,'mobile')){
                    $q->whereLike("mobile","%{$request['mobile']}%");
                }
                if(check_request($request,'is_writer')){
                    $q->where("is_writer",$request['is_writer']);
                }
                if(check_request($request,'user_id')){
                    $q->where("id",$request['user_id']);
                }
                if(check_request($request,'vip_id')){
                    $q->where("vip_id",$request['vip_id']);
                }
                if(check_request($request,'is_vip')){
                    $q->where("vip_id",'>',0);
                }
                if(check_request($request,'is_black')){
                    $q->where("is_black",$request['is_black']);
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query->field($filed)->order($order)->page($page,$limit)->select()->toArray();
        $count = $query->count();
        return [$list,$count];
    }

}